package com.example.demo.zaqizabatest.leetcode.day1;

import java.util.Stack;

/**
 * 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，
 * 分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class CQueue {

    /**
     * //初始化
     * Stack<Integer> stack = new Stack<Integer>();
     * //进栈
     * stack.push(Element);
     * //出栈
     * stack.pop();
     * //取栈顶值（不出栈）
     * stack.peek();
     * //判断栈是否为空
     * stack.isEmpty()
     */
    private Stack<Integer> stack1 = new Stack<Integer>();

    private Stack<Integer> stack2 = new Stack<Integer>();

    CQueue() {

    }

    void appendTail(int value) {
        if (stack1.empty() && stack2.empty()) {
            stack1.push(value);
            return;
        }
        if (!stack1.empty()) {
            stack1.push(value);
            return;
        }
//        if (!stack2.empty()) {
//            stack2.push(value);
//            return;
//        }
    }

    int deleteHead() {
        if (stack1.empty() && stack2.empty()) {
            return -1;
        }
        int value = 0;
        if (!stack1.empty()) {
            if (stack1.size() == 1){
                value = stack1.peek();
                stack1.clear();
                stack2.clear();
                return value;
            }
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
            value = stack2.pop();
            while (!stack2.empty()) {
                stack1.push(stack2.pop());
            }
            return value;
        }
        return -1;
    }

    public static void main(String[] args) {
        CQueue cQueue = null;
        String[] str = new String[]{"CQueue", "deleteHead", "appendTail", "deleteHead", "appendTail", "appendTail", "deleteHead", "deleteHead", "deleteHead", "appendTail", "deleteHead", "appendTail", "appendTail", "appendTail", "appendTail", "appendTail", "appendTail", "deleteHead", "deleteHead", "deleteHead", "deleteHead"};
        Integer[] list = new Integer[]{
                null, null, 12, null, 10, 9, null, null, null, 20, null, 1, 8, 20, 1, 11, 2, null, null, null, null
        };
        for (int i = 0; i < str.length; i++) {
            if ("deleteHead".equals(str[i])) {
                System.out.println(cQueue.deleteHead());
            }
            if ("appendTail".equals(str[i])) {
                cQueue.appendTail(list[i]);
            }
            if ("CQueue".equals(str[i])) {
                cQueue = new CQueue();
            }
        }

    }
}
